#include "mainwindow.h"
#include "qfiledialog.h"
#include "qstringlist.h"   
#include "qspinbox.h"
#include "qpixmap.h"
#include "qstring.h"


using namespace Qt;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);

    ui->treeWidget->clear();
    QTreeWidgetItem* header = new QTreeWidgetItem();
    header->setText(column_item_0, "目录和文件");
    header->setText(column_type_1, "节点类型");
    header->setText(column_date_2, "最后的修改日期");

    //对其方式
    header->setTextAlignment(0, Qt::AlignHCenter | Qt::AlignVCenter);

    ui->treeWidget->setHeaderItem(header);//设置树的“表头”

    //创建一个根节点
    QTreeWidgetItem* topItem = new QTreeWidgetItem(top_item);
    topItem->setText(column_item_0, "图片");
    topItem->setFlags(ItemIsSelectable | ItemIsUserCheckable | ItemIsEnabled | ItemIsAutoTristate);
    topItem->setCheckState(column_item_0, Checked);
    ui->treeWidget->addTopLevelItem(topItem);//添加到树控件中

    label_node_text = new QLabel("节点标题", this);
    label_node_text->setMinimumWidth(200);
    ui->statusBar->addWidget(label_node_text);

    spin_box = new QSpinBox(this);
    spin_box->setRange(0, 2000);
    spin_box->setValue(100);
    spin_box->setSuffix(" %");
    spin_box->setReadOnly(true);//不能修改
    spin_box->setButtonSymbols(QAbstractSpinBox::NoButtons);//取消按钮标志
    ui->statusBar->addPermanentWidget(spin_box);

    label_file_name = new QLabel("文件名", this);
    ui->statusBar->addPermanentWidget(label_file_name);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::display_image(QTreeWidgetItem* item)
{
    QString filename = item->data(column_item_0, UserRole).toString();
    label_file_name->setText(filename);
    label_node_text->setText(item->text(column_item_0));
    pixmap.load(filename);
    ui->action_FitWideBest->triggered();
    ui->action_FitWideBest->setEnabled(true);
    ui->action_FitHeightBest->triggered();
    ui->action_FitHeightBest->setEnabled(true);

    ui->action_ZoomIn->setEnabled(true);
    ui->action_ZoomOut->setEnabled(true);
    ui->action_RealSize->setEnabled(true);
}

void MainWindow::change_item_caption(QTreeWidgetItem* item)
{
    QString str = '*' + item->text(column_item_0);
    item->setText(column_item_0, str);
    if (item->childCount() > 0)
        for (int i = 0; i < item->childCount(); i++)
            change_item_caption(item->child(i));
}

void MainWindow::on_action_AddFiles_triggered()
{
    QStringList files = QFileDialog::getOpenFileNames(this, "选择文件","C:\\Users\\CaiYunWei\\Desktop", "Images(*.jpg)");
    if (files.isEmpty())return;

    QTreeWidgetItem* parItem = nullptr;
    QTreeWidgetItem* item = nullptr;

    item = ui->treeWidget->currentItem();
    if (item == nullptr)
        item = ui->treeWidget->topLevelItem(0);
    if (item->type() == image_item)
        parItem = item->parent();
    else
        parItem = item;

    QString fileName;
    QFileInfo fileInfo;
    QDateTime dateTime;
    QString nodeText;
    QTreeWidgetItem** it_item = new QTreeWidgetItem * [files.size()];
    for (int i = 0; i < files.size(); i++)
    {
        fileName = files.at(i);
        fileInfo.setFile(files.at(i));
        dateTime = fileInfo.lastModified();
        nodeText = fileInfo.fileName();

        //使用[]会错误的提示警告
        *(it_item+i) = new QTreeWidgetItem(image_item);
        (*(it_item + i))->setText(column_item_0, nodeText);
        (*(it_item + i))->setText(column_type_1, "Image Item");
        (*(it_item + i))->setText(column_date_2, dateTime.toString("yyyy-MM-dd"));
        (*(it_item + i))->setData(column_item_0, UserRole, QVariant(fileName));
        parItem->addChild(*(it_item + i));

        //不能delete 很神奇，一delete就把项删除了，看来添加并不是信息复制
        /*delete it_item;*/
        //应该是把它加入到了TreeWidget里，而TreeWidget挂到MainWindow上了，关闭Qt的树就把它删了

    }

    parItem->setExpanded(true);//自动展开

}

void MainWindow::on_action_DeleteItem_triggered()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if (item == nullptr) 
        return;
    QTreeWidgetItem* parItem = item->parent();//没有爹返回nullptr
    if (parItem == nullptr)
        return;
    parItem->removeChild(item);
}

void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
{
    if (current == nullptr || current == previous)//现在为空，或现在选择的和之前一样
        return;

    switch (current->type())
    {
    case top_item:
        ui->action_AddFolder->setEnabled(true);
        ui->action_AddFiles->setEnabled(true);
        ui->action_DeleteItem->setEnabled(false);//最顶层的不应删除
        break;
    case group_item:
        ui->action_AddFolder->setEnabled(true);
        ui->action_AddFiles->setEnabled(true);
        ui->action_DeleteItem->setEnabled(true);
        break;
    case image_item:
        ui->action_AddFolder->setEnabled(false);//image（最后的节点）不应往下添加目录
        ui->action_AddFiles->setEnabled(true);
        ui->action_DeleteItem->setEnabled(true);

        //显示图片
        display_image(ui->treeWidget->currentItem());

        break;
    default:
        break;
    }

}

void MainWindow::on_action_ScanItems_triggered()
{
    for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    {
        change_item_caption(ui->treeWidget->topLevelItem(i));
    }
}

void MainWindow::on_action_FitWideBest_triggered()
{
    int w = ui->scrollArea->width() - 30;
    int realw = pixmap.width();
    ratio = float(w) / realw;

    spin_box->setValue(ratio * 100);
    QPixmap pix = pixmap.scaledToWidth(w - 30);
    ui->labelPicture->setPixmap(pix);
}

void MainWindow::on_action_FitHeightBest_triggered()
{
    int h = ui->scrollArea->height() - 30;
    int realh = pixmap.height();
    ratio = float(h) / realh;

    spin_box->setValue(ratio * 100);
    QPixmap pix = pixmap.scaledToHeight(h - 30);
    ui->labelPicture->setPixmap(pix);
}

/**
 * @brief 添加目录行为触发
 */
void MainWindow::on_action_AddFolder_triggered()
{
    QString dir = QFileDialog::getExistingDirectory();
    if (dir.isEmpty())
        return;

    QTreeWidgetItem* parItem = ui->treeWidget->currentItem();
    if (parItem == nullptr)
        return;

    if (parItem->type() != image_item)
    {
        int cnt = dir.length();
        int i = dir.lastIndexOf("/");
        QString nodeName = dir.right(cnt - i - 1);
        //添加一个节点
        QTreeWidgetItem* item = new QTreeWidgetItem(group_item);
        item->setText(column_item_0, nodeName);
        item->setText(column_type_1, "Group Item");
        item->setFlags(ItemIsSelectable | ItemIsUserCheckable | ItemIsEnabled | ItemIsAutoTristate);
        item->setCheckState(column_item_0, Checked); //复选框
        item->setData(column_item_0, UserRole, QVariant(dir));
        parItem->addChild(item);
        
    }
}

/**
 * @brief 实际大小行为触发
 */
void MainWindow::on_action_RealSize_triggered()
{
    ui->labelPicture->setPixmap(pixmap);
    ratio = 1;
    spin_box->setValue(100);
}

/**
 * @brief 放大行为触发
 */
void MainWindow::on_action_ZoomIn_triggered()
{
    ratio *= 1.2;
    int w = ratio * pixmap.width();
    int h = ratio * pixmap.height();
    ui->labelPicture->setPixmap(pixmap.scaled(w,h));
    spin_box->setValue(100 * ratio);
}

/**
 * @brief 缩小行为触发
 */
void MainWindow::on_action_ZoomOut_triggered()
{
    ratio *= 0.8;
    int w = ratio * pixmap.width();
    int h = ratio * pixmap.height();
    ui->labelPicture->setPixmap(pixmap.scaled(w, h));
    spin_box->setValue(100 * ratio);
}

/**
 * @brief 可见行为触发
 */
void MainWindow::on_action_Visible_triggered(bool checked)
{
    ui->dockWidget->setVisible(checked);
}

/**
 * @brief 浮动行为触发
 */
void MainWindow::on_action_Float_triggered(bool checked)
{
    ui->dockWidget->setFloating(checked);
}

void MainWindow::on_dockWidget_visibilityChanged(bool visible)
{
    ui->action_Visible->setChecked(visible);
}

void MainWindow::on_dockWidget_topLevelChanged(bool topLevel)
{
    ui->action_Float->setChecked(topLevel);
}


